package com.finalist.controller.admin;


import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.finalist.Message;
import com.finalist.entity.Destination;
import com.finalist.service.DestinationService;

@Controller("destinationController")
@RequestMapping("/admin/destination")
public class DestinationController extends BaseController{
	
	
	@Resource(name = "destinationServiceImpl")
	private DestinationService destinationService;
	/**
	 * 列表 按中文首字母排序
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(ModelMap model) {
		List<Destination> newDestination = new ArrayList<Destination>();
		List<Destination> destinations = destinationService.findAll();
		newDestination.addAll(destinations);
		
		Collections.sort(newDestination,new Comparator<Destination>(){
			public int compare(Destination o1, Destination o2) {
			       String s1 = o1.getName();
			            String s2 = o2.getName();
			            return Collator.getInstance(Locale.CHINESE).compare(s1, s2); 
			}
			        });
		model.addAttribute("destinations",newDestination );
		return "/admin/destination/list";
	}
	
	/**
	 * 添加
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap model) {
		
		return "/admin/destination/add";
	}
	
	/**
	 * 保存
	 * @param destination
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public String save(Destination destination,RedirectAttributes redirectAttributes){
		destinationService.save(destination);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}
	
	/**
	 * 编辑
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value ="/edit",method = RequestMethod.GET)
	public String edit(Long id,ModelMap model){
		model.addAttribute("destination",destinationService.find(id));
		return "/admin/destination/edit";
	}
	
	/**
	 * 更新
	 * @param destination
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public String update(Destination destination,RedirectAttributes redirectAttributes){
		destinationService.update(destination);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	Message delete(Long id) {
		destinationService.delete(id);
		return SUCCESS_MESSAGE;
	}
	
	@RequestMapping(value = "/check_name", method = RequestMethod.GET)
	public @ResponseBody
	boolean checkName(String name) {
		if (StringUtils.isEmpty(name)) {
			return false;
		}
		if (destinationService.nameExists(name)) {
			return false;
		} else {
			return true;
		}
	}
	
}
